package com.pan.hisystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.service.IUserRoleService;
import com.pan.hisystem.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingyang
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @ApiOperation(value = "添加操作员")
    @PostMapping("/addUser")
    public RespBean addUser(@RequestBody User user){
        String password=passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        return userService.addUserWithRole(user);
    }

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/getUsers")
    public List<User> getUsers(){
        return userService.list();
    }

    @ApiOperation(value = "获取所有操作员（有角色）")
    @GetMapping("/getUsersWithRoles")
    public List<User> getUsersWithRoles(){
        return userService.getUsersWithRoles();
    }
    @ApiOperation(value = "根据姓名获取操作员信息")
    @GetMapping("/getUserByName/{name}")
    public List<User> getUserByName(@PathVariable String name){
        return userService.getUserByName(name);
    }

    @ApiOperation(value = "更新操作员")
    @PutMapping("/updateUser")
    public RespBean updateUser(@RequestBody User user){
        if(userService.updateById(user)){
            return RespBean.success("修改成功!");
        }
        return RespBean.error("修改失败!");
    }

    @ApiOperation(value = "更新操作员及其角色")
    @PutMapping("/updateUserWithRole")
    public RespBean updateUserWithRole(@RequestBody User user){
        System.out.println(user.toString());
        return userService.updateUserWithRole(user);
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/deleteUser/{id}")
    public RespBean deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id);
    }
    @ApiOperation(value = "批量删除操作员")
    @DeleteMapping("/deleteUsers")
    public RespBean deleteUsers(Integer[] ids){
        if(userService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "获取操作员角色")
    @GetMapping("/{uid}")
    public List<Role> getUserRoles(@PathVariable Integer uid){
        return userService.getUserRoles(uid);
    }

    @ApiOperation(value = "添加操作员角色")
    @PostMapping("/addRole")
    public RespBean addRole(@RequestBody UserRole userRole){
        userRole.setCreateTime(LocalDateTime.now());
        if(userRoleService.save(userRole)){
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }
    @ApiOperation(value = "删除操作员角色")
    @DeleteMapping("/deleteRole/{id}")
    public RespBean deleteRole(@PathVariable Integer id){
        if(userRoleService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    /*@ApiOperation(value = "获取角色为医生的操作员")
    @GetMapping("/getUserWithDoctor")
    public List<User> getUserWithDoctor(@RequestParam(value = "did") Integer did,@RequestParam(value = "tid") Integer tid,@RequestParam(value = "mid")String mid){
        return userService.getUserWithDoctor(did,tid,mid);
    }*/

    /*@ApiOperation(value = "获取操作员")
    @GetMapping("/getUser")
    public User getUser(Principal principal){
        if(principal==null){
            return null;
        }
        User user =userService.getOne(new QueryWrapper<User>().eq("username",principal.getName()));
        user.setRoles(userService.getRoles(user.getId()));
        return user;
    }*/



}
